home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / comm / term / term_source.lha / Extras / Source / term-source.lha / Memory.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  2KB  |  121 lines

  1. /*
  2. **    Memory.c
  3. **
  4. **    Memory management routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* Main memory pool puddle size */
  15.  
  16. #define PUDDLE_SIZE 32768
  17.  
  18.     /* Memory allocation data */
  19.  
  20. STATIC struct SignalSemaphore    MemorySemaphore;
  21. STATIC APTR            MemoryPool;
  22.  
  23.     /* MemorySetup():
  24.      *
  25.      *    Set up the main memory pool.
  26.      */
  27.  
  28. BOOL
  29. MemorySetup()
  30. {
  31.     InitSemaphore(&MemorySemaphore);
  32.  
  33.     if(MemoryPool = AsmCreatePool(MEMF_PUBLIC | MEMF_ANY,PUDDLE_SIZE,PUDDLE_SIZE,SysBase))
  34.         return(TRUE);
  35.     else
  36.         return(FALSE);
  37. }
  38.  
  39.     /* MemoryCleanup():
  40.      *
  41.      *    Clean up the main memory pool.
  42.      */
  43.  
  44. VOID
  45. MemoryCleanup()
  46. {
  47.     if(MemoryPool)
  48.     {
  49.         AsmDeletePool(MemoryPool,SysBase);
  50.  
  51.         MemoryPool = NULL;
  52.     }
  53. }
  54.  
  55.     /* FreeVecPooled(APTR Memory):
  56.      *
  57.      *    Free a memory chunk.
  58.      */
  59.  
  60. VOID
  61. FreeVecPooled(APTR Memory)
  62. {
  63.     if(Memory && MemoryPool)
  64.     {
  65.         ULONG *Data = (ULONG *)Memory;
  66.  
  67.         ObtainSemaphore(&MemorySemaphore);
  68.  
  69.         AsmFreePooled(MemoryPool,&Data[-1],Data[-1],SysBase);
  70.  
  71.         ReleaseSemaphore(&MemorySemaphore);
  72.     }
  73. }
  74.  
  75.     /* AllocVecPooled(ULONG Size,ULONG Flags):
  76.      *
  77.      *    Allocate a memory chunk from the main memory pool and
  78.      *    remember its size.
  79.      */
  80.  
  81. APTR
  82. AllocVecPooled(ULONG Size,ULONG Flags)
  83. {
  84.     if(MemoryPool && Size > 0)
  85.     {
  86.         ULONG *Data;
  87.  
  88.         ObtainSemaphore(&MemorySemaphore);
  89.         Data = (ULONG *)AsmAllocPooled(MemoryPool,Size + sizeof(ULONG),SysBase);
  90.         ReleaseSemaphore(&MemorySemaphore);
  91.  
  92.         if(Data)
  93.         {
  94.             *Data++ = Size + sizeof(ULONG);
  95.  
  96.                 /* Zero the chunk if necessary */
  97.  
  98.             if(Flags & MEMF_CLEAR)
  99.                 memset(Data,0,Size);
  100.         }
  101.  
  102.         return((APTR)Data);
  103.     }
  104.     else
  105.     {
  106.         struct Process *CurrentProcess;
  107.  
  108.             /* If a process is making the call,
  109.              * set the secondary error code to
  110.              * something meaningful.
  111.              */
  112.  
  113.         CurrentProcess = (struct Process *)FindTask(NULL);
  114.  
  115.         if(CurrentProcess->pr_Task.tc_Node.ln_Type == NT_PROCESS)
  116.             SetIoErr(ERROR_NO_FREE_STORE);
  117.  
  118.         return(NULL);
  119.     }
  120. }
  121.